package com.vision.sales.cm.data;

import com.vision.core.cm.util.DateUtils;
import com.vision.sales.cm.db.data.CreditMemo;
import com.vision.sales.cm.db.data.DebitMemo;
import com.vision.sales.cm.db.data.SalesInvoice;

/**
 * 
 * @author Mark
 *
 */
public class DataUtils {
	
	public static String checkCreditMemo(CreditMemo memo) {
		SalesInvoice invoice = memo.getSalesInvoice();
		
		if (!invoice.getCompany().equals(memo.getCompany())) {
			return "Selected company and sales invoice company must match.";
		}
		
		if (!invoice.getCustomer().equals(memo.getCustomer())) {
			return "Selected customer and sales invoice customer must match.";
		}
		
		if (DateUtils.isBefore(memo.getDate(), invoice.getDate())) {
			return "Credit memo date must be after or same as sales invoice date.";
		}
		
		// If the type is BAD_DEBTS, must have a selected sales invoice
		// and sales invoice.balance must be the same as the amount
		if (memo.getType() == CreditMemoType.BAD_DEBTS) {
			if (memo.getAmount() != invoice.getBalance()) {
				return "For bad debts, amount must be equal to the sales invoice balance.";
			}
		}
		
		return null;
	}
	
	public static String checkDebitMemo(DebitMemo memo) {
		SalesInvoice invoice = memo.getSalesInvoice();
		
		if (!invoice.getCompany().equals(memo.getCompany())) {
			return "Selected company and sales invoice company must match.";
		}
		
		if (!invoice.getCustomer().equals(memo.getCustomer())) {
			return "Selected customer and sales invoice customer must match.";
		}
		
		if (DateUtils.isBefore(memo.getDate(), invoice.getDate())) {
			return "Debit memo date must be after or same as sales invoice date.";
		}
		
		return null;
	}

}
